package com.hbnu.mysql;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;

public class DatabaseDemo7 {

    //在Demotest中测试后，为了防止sql注入，使用preparedStatement
    //此文件为增添数据库内容
    public static void main(String[] args) {

        try {
            Class.forName("com.mysql.cj.jdbc.Driver");

            String url = "jdbc:mysql://localhost:3306/class2211?serverTimezone=UTC&useSSL=false";
            String user = "root";
            String password = "12123";

            Connection connection = DriverManager.getConnection(url,user,password);


            String sql = "insert into nncc1(c1,c2,c3) values (?,?,?)";
            PreparedStatement preparedStatement =  connection.prepareStatement(sql);

            preparedStatement.setString(1,"3");             //前面的数字是第几个占位符的意思
            preparedStatement.setString(2,"6");
            preparedStatement.setString(3,"9");

            int rows = preparedStatement.executeUpdate();
            System.out.println("影响了数据库表"+rows+"条数据");

            preparedStatement.close();
            connection.close();


        } catch (Exception e) {
            throw new RuntimeException(e);
        }

    }
}
